/*
 * Copyright (C) 2011 JiangHongTiao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.paris.helperutils.image;

import sk.lieskove.jianghongtiao.common.utils.text.Diacritics;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xjuraj
 */
public class TextToImage {

    private static final String SAVE_PATH = "/home/xjuraj/Projects/master-thesis"
            + "/documents/master-thesis/my/images/diacritics/";
    private static final int size = 60;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        diacritics2Latex();
    }

    private static void diacritics2Latex() {
        for (Iterator it = Diacritics.DIACRITICS_MAP.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            if (Character.isLowerCase(key.charAt(0))) {
                continue;
            }
            System.out.println("\\hline " + key + " \\cite{wikiLetter"+key+"} & \\vspace*{-9pt} ");
            diacriticsForLetter(key);
            System.out.println(" & " + key.toLowerCase() + " & \\vspace*{-9pt}");
            diacriticsForLetter(key.toLowerCase());
            System.out.println("\\\\");
        }
    }

    private static void diacriticsForLetter(String key) {
        String letters = Diacritics.DIACRITICS_MAP.get(key);
        for (int i = 0; i < letters.length(); i++) {
            char charAt = letters.charAt(i);
            String name = key + ((i < 10) ? "-0" + i : "-" + i) + ".png";
            writeText(String.valueOf(charAt), name);
            System.out.println("			\\includegraphics[height=11pt]{" + name + "} ");
        }
    }

    private static void writeText(String text, String fileName) {
        Font font = new Font("LMRoman10", Font.PLAIN, size);
        try {
            ImageIO.write(createTextImage(text, font), "png",
                    new FileOutputStream(new File(SAVE_PATH + fileName)));
        } catch (IOException ex) {
            Logger.getLogger(TextToImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static BufferedImage createTextImage(String text, Font font) {
        //predpocitat velikost napisu
        BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setFont(font);
        Rectangle2D sb1 = g.getFontMetrics().getStringBounds(text, g);
        int sirka = (int) sb1.getWidth();
        int vyska = (int) sb1.getHeight();

        //vytvorit napis
        image = new BufferedImage(sirka, vyska, BufferedImage.TYPE_INT_RGB);
        g = image.createGraphics();
        g.setFont(font);
        //vymazat
        g.setBackground(Color.WHITE);
        g.fillRect(0, 0, sirka, vyska);
        g.setColor(Color.BLACK);

        TextLayout tl = new TextLayout(text, font, g.getFontRenderContext());
        Rectangle2D sb = tl.getBounds();
        int x = sirka / 2 - (int) sb.getX() - (int) (sb.getWidth() / 2);
        int y = vyska / 2 - (int) sb.getY() - (int) (sb.getHeight() / 2);
        tl.draw(g, x, y);
        return image;
    }
}
